import * as Types from 'containers/ActionTypes';
import isMobile from 'ismobilejs';
import Actions from 'containers/Actions';
import * as RootService from 'services/RootService';

export function detectEnvironment() {
  return {
    type: Types.ROOT.DETECT_ENVIRONMENT,
    isMobile: isMobile.any,
  };
}

export function getCategories() {
  return dispatch => RootService.getCategorieList()
    .then((data) => {
      dispatch({
        type: Types.ROOT.RECEIVE_CATEGORIES,
        categories: data.categories,
      });
      return data.categories;
    });
}

export function loginFromCookies() {
  return Actions.SessionAction.loginFromCookies();
}

export function signOut() {
  return Actions.SessionAction.signout();
}

export function getMyWorkStats() {
  return (dispatch) => {
    dispatch(Actions.PublishedAction.getMyPublishedWorks());
    dispatch(Actions.UploadsAction.getMyUploadsWorks());
    dispatch(Actions.LikesAction.getMyLikedWorks());
  };
}

export function toggleSidebarDisplay() {
  return {
    type: Types.ROOT.TOGGLE_SIDEBAR,
  };
}

export function hideSidebarDisplay() {
  return (dispatch, getState) => {
    const { Root } = getState();
    if (Root.sidebarVisibility === true) {
      dispatch({
        type: Types.ROOT.HIDE_SIDEBAR,
      });
    }
  };
}
